using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyControl : MonoBehaviour
{
    public bool isChange;//控制方向开关
    public float speed;
    private Vector2 way;//方向
    private Rigidbody2D rb;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        way = isChange ? Vector2.up : Vector2.right;
        time = 2;
    }
    private float time;//定时器
    private float times = 2;//归位器
    void FixedUpdate()
    {
        time -= Time.deltaTime;
        if (time < 0)
        {
            speed = -speed;
            time = times;
        }
        Vector2 position = rb.position;
        position.x += way.x * speed * Time.deltaTime;
        position.y += way.y * speed * Time.deltaTime;
        rb.MovePosition(position);
    }
    void OnCollisionEnter2D(Collision2D other)
    {
        moveControll pc = other.gameObject.GetComponent<moveControll>();
        if (pc != null)
        {
            Debug.Log("玩家碰撞到敌人");
            if (pc.CurrentHealth < pc.MaxHealth)
            {
                
                pc.deleteHealth(1);
                Debug.Log("当前血量/最大血量: " + pc.CurrentHealth + "/" + pc.MaxHealth);

            }
            
        }
    }
    
    
}
